var ImageNumber;

	function loadGalleryImages(sender, args) {
	
		// load the images from the array into
		// the gallery canvases
		
		var imageName;
		
		for (var i=0; i<PhotoData.length; i++) {
			if (i < 10) {
				imageName = "imageBrush0" + i;
			}
			else {
				imageName = "imageBrush" + i;
			}
			//sender.findName(imageName).ImageSource = gx.util.resourceUrl(gx.basePath + gx.staticDirectory + PhotoData[i].Image,true);
			sender.findName(imageName).ImageSource = PhotoData[i].Image;
			
		}
		
		for (var i=8; i<PhotoData.length; i++) {
			if (i < 10) {
				var imageName = "reflectionBrush0" + i;
			}
			else {
				var imageName = "reflectionBrush" + i;
			}
			//sender.findName(imageName).ImageSource = gx.util.resourceUrl(gx.basePath + gx.staticDirectory + PhotoData[i].Image,true);
			sender.findName(imageName).ImageSource = PhotoData[i].Image;
		}
	
		sender.findName("displayImage").ImageSource = PhotoData[0].Image;//gx.util.resourceUrl(gx.basePath + gx.staticDirectory + PhotoData[0].Image,true);
		sender.findName("displayImagePaneReflectionImage").ImageSource = PhotoData[0].Image;//gx.util.resourceUrl(gx.basePath + gx.staticDirectory + PhotoData[0].Image,true);
		//sender.findName("galleryTitleText").text = galleryTitle;
	
		sender.findName("labelText").text = PhotoData[0].Description;
		sender.findName("labelTextShadow").text = PhotoData[0].Description;
		sender.findName("labelTextReflection").text = PhotoData[0].Description;
		sender.findName("labelTextShadowReflection").text = PhotoData[0].Description;
		var textCenter = sender.findName("rightWall").width / 2;
		var labelCenter = textCenter - (sender.findName("labelText").ActualWidth / 2);
		
		sender.findName("labelText")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadow")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextReflection")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadowReflection")["Canvas.Left"] = labelCenter;
	
		sender.findName("timer").begin();
		sender.findName("timer").addEventListener("completed", doSlideshow);
	}
	
	
	function showOutline(sender,args) {
		sender.strokeThickness = 2;
		sender.stroke = "#000000";
	
		outlineObjectName = sender.name;
		outlineImageNumber = outlineObjectName.substring(5, outlineObjectName.length);
	
		if (outlineImageNumber >= 8) {	
			var reflection = "reflectionImage" + outlineImageNumber;
	
			sender.findName(reflection).strokeThickness = 2;
			sender.findName(reflection).stroke = "#000000";
		}
	}
	
	function hideOutline(sender,args) {
		sender.strokeThickness = 0;
	
		outlineObjectName = sender.name;
		outlineImageNumber = outlineObjectName.substring(5, outlineObjectName.length);
	
		if (outlineImageNumber >= 8) {	
			var reflection = "reflectionImage" + outlineImageNumber;
			sender.findName(reflection).strokeThickness = 0;
		}
	}
	
	function displayImage(sender,args) {
		
		sender.findName("timer").stop();
		objectName = sender.name;
		imageNumber = objectName.substring(5, objectName.length);
		
		
		imageName = "imageBrush" + imageNumber;	
		sender.findName("fadeOutImage").begin();
		sender.findName("fadeOutImage").addEventListener("completed", showNextImage);
		
	}
	
	function showNextImage(sender, args) {
		imageNumber *= 1;
		MyUserControl.SelectedPhotoId = PhotoData[imageNumber].Id;
		
		sender.findName("labelText").text = PhotoData[imageNumber].Description;
		sender.findName("labelTextShadow").text = PhotoData[imageNumber].Description;
		sender.findName("labelTextReflection").text = PhotoData[imageNumber].Description;
		sender.findName("labelTextShadowReflection").text = PhotoData[imageNumber].Description;
		var textCenter = sender.findName("rightWall").width / 2;
		var labelCenter = textCenter - (sender.findName("labelText").ActualWidth / 2);
		
		sender.findName("labelText")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadow")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextReflection")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadowReflection")["Canvas.Left"] = labelCenter;	
		
		sender.findName("displayImage").ImageSource = sender.findName(imageName).imageSource;
		sender.findName("displayImagePaneReflectionImage").ImageSource = sender.findName(imageName).imageSource;
		sender.findName("fadeInImage").begin();
		sender.findName("timer").begin();
		if (MyUserControl.PhotoChanged!=undefined)
		{
			MyUserControl.PhotoChanged();
	  }
		
	}
	
	function doSlideshow(sender, args) {
	
		// find out which image is currently being displayed
		objectName = sender.findName("displayImage").ImageSource;
		for (var i=0; i<PhotoData.length; i++) {
			if (objectName == PhotoData[i]) {
				imageNumber = i;
				break;
			}
		}
	
		nextImageToDisplay = imageNumber + 1;
		displayImages(sender, args);
		sender.findName("timer").begin();
	}
	
	function displayImages(sender, args) {
		sender.findName("fadeOutImage").begin();
		fadeOutCompleted = sender.findName("fadeOutImage").addEventListener("completed", nextSlide);
	}
	
	function nextSlide(sender, args) {
		sender.findName("fadeOutImage").removeEventListener("completed", fadeOutCompleted);
		
		if (nextImageToDisplay == (PhotoData.length)) {
			nextImageToDisplay = 0;
		}
		
		if (nextImageToDisplay < 10) {
			var imageName = "imageBrush0" + nextImageToDisplay;
		}
		else {
			var imageName = "imageBrush" + nextImageToDisplay;
		}
	
		sender.findName("labelText").text = PhotoData[nextImageToDisplay].Description;
		sender.findName("labelTextShadow").text = PhotoData[nextImageToDisplay].Description;
		sender.findName("labelTextReflection").text = PhotoData[nextImageToDisplay].Description;
		sender.findName("labelTextShadowReflection").text = PhotoData[nextImageToDisplay].Description;
		var textCenter = sender.findName("rightWall").width / 2;
		var labelCenter = textCenter - (sender.findName("labelText").ActualWidth / 2);
		
		sender.findName("labelText")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadow")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextReflection")["Canvas.Left"] = labelCenter;
		sender.findName("labelTextShadowReflection")["Canvas.Left"] = labelCenter;
		
		sender.findName("displayImage").ImageSource = sender.findName(imageName).imageSource;
		sender.findName("displayImagePaneReflectionImage").ImageSource = sender.findName(imageName).imageSource;
		sender.findName("fadeInImage").begin();
	}


